setwd("~/Dropbox/DATA/DIPUTADOS_DISTRITO")
rm(list = ls(all = TRUE)) #limpiar workspace

library(plyr)

## Functions
int <- function(data, parties) {
  out <- grep(parties[1],names(data)):grep(parties[2],names(data))
  return(out)
}
winner <- function(x) { names(which.max(x)) }

## 1991
data <- read.table("DIPUTADOS_1991.txt", header=T, sep="\t")
names(data)
votes.91 <- apply(data[,c("PAN","PRI","PRD")], 2, sum, na.rm=T)/sum(data$TOTAL, na.rm=T)
foo <- apply(data[,int(data, c("PAN","PT"))], 1, winner)
seats.91 <- table(foo)[c("PAN","PRI","PRD")]

## 1994
data <- read.table("DIPUTADOS_1994.txt", header=T, sep="\t")
data <- data[complete.cases(data),]
names(data)
votes.94 <- apply(data[,c("PAN","PRI","PRD")], 2, sum, na.rm=T)/sum(data$TOTAL, na.rm=T)
foo <- apply(data[,int(data, c("PAN","PVEM"))], 1, winner)
seats.94 <- table(foo)[c("PAN","PRI","PRD")]

## 1997
data <- read.table("DIPUTADOS_1997.txt", header=T, sep="\t")
names(data)
votes.97 <- apply(data[,c("PAN","PRI","PRD")], 2, sum, na.rm=T)/sum(data$TOTAL, na.rm=T)
foo <- apply(data[,int(data, c("PAN","PDM"))], 1, winner)
seats.97 <- table(foo)[c("PAN","PRI","PRD")]

## 2000
data <- read.table("DIPUTADOS_2000.txt", header=T, sep="\t")
names(data)
#begin fix---
data <- rename(data, c("AC"="PAN", "AM"="PRD"))
#end fix-----
votes.00 <- apply(data[,c("PAN","PRI","PRD")], 2, sum, na.rm=T)/sum(data$TOTAL, na.rm=T)
foo <- apply(data[,int(data, c("PAN","DSPPN"))], 1, winner)
seats.00 <- table(foo)[c("PAN","PRI","PRD")]

## 2003
data <- read.table("DIPUTADOS_2003.txt", header=T, sep="\t")
names(data)
#begin fix---
data[!is.na(data$APT),"PRI"] <- data[!is.na(data$APT),"APT"]
data$PVEM[is.na(data$PVEM)] <- 0
data$APT <- NULL
names(data)
#end fix-----
votes.03 <- apply(data[,c("PAN","PRI","PRD")], 2, sum, na.rm=T)/sum(data$TOTAL, na.rm=T)
foo <- apply(data[,int(data, c("PAN","FC"))], 1, winner)
seats.03 <- table(foo)[c("PAN","PRI","PRD")]

## 2006
data <- read.table("DIPUTADOS_2006.txt", header=T, sep="\t")
names(data)
#begin fix---
data <- rename(data, c("ALIANZA.POR.MÉXICO"="PRI", "POR.EL.BIEN.DE.TODOS"="PRD"))
#end fix-----
votes.06 <- apply(data[,c("PAN","PRI","PRD")], 2, sum, na.rm=T)/sum(data$TOTAL, na.rm=T)
foo <- apply(data[,int(data, c("PAN","ALTERNATIVA"))], 1, winner)
seats.06 <- table(foo)[c("PAN","PRI","PRD")]

## 2009
data <- read.table("DIPUTADOS_2009.txt", header=T, sep="\t")
names(data)
#begin fix---
is.coal <- !is.na(data$PRIMERO.MÉXICO)
data[is.coal,"PRI"] <- rowSums(data[is.coal, c("PRI","PVEM","PRIMERO.MÉXICO")])
data$PVEM[is.coal] <- 0
data$PRIMERO.MÉXICO <- NULL
data$SALVEMOS.MÉXICO <- rowSums(data[,c("PT","CONVERGENCIA","SALVEMOS.MÉXICO")])
data$PT <- NULL
data$CONVERGENCIA <- NULL
names(data)
#end fix-----
votes.09 <- apply(data[,c("PAN","PRI","PRD")], 2, sum, na.rm=T)/sum(data$TOTAL, na.rm=T)
foo <- apply(data[,int(data, c("PAN","SALVEMOS.MÉXICO"))], 1, winner)
seats.09 <- table(foo)[c("PAN","PRI","PRD")]

## 2012
data <- read.table("DIPUTADOS_2012.txt", header=T, sep="\t")
names(data)
#begin fix---
is.coal <- data$COALICION.PRI.PVEM != 0
data[is.coal,"PRI"] <- rowSums(data[is.coal, c("PRI","PVEM","COALICION.PRI.PVEM")])
data$PVEM[is.coal] <- 0
data$COALICION.PRI.PVEM <- NULL
data$PRD <- rowSums(data[,c("PRD","PT","MOVIMIENTO.CIUDADANO",
                            "COALICION.PRD.PT.MC","COALICION.PRD.PT",
                            "COALICION.PRD.MC","COALICION.PT.MC")])
data$PT <- NULL
data$MOVIMIENTO.CIUDADANO <- NULL
data$COALICION.PRD.PT.MC <- NULL
data$COALICION.PRD.PT <- NULL
data$COALICION.PRD.MC <- NULL
data$COALICION.PT.MC <- NULL
names(data)
#end fix-----
votes.12 <- apply(data[,c("PAN","PRI","PRD")], 2, sum, na.rm=T)/sum(data$TOTAL, na.rm=T)
foo <- apply(data[,int(data, c("PAN","NUEVA.ALIANZA"))], 1, winner)
seats.12 <- table(foo)[c("PAN","PRI","PRD")]

# Append data
votes <- rbind(votes.91, votes.94, votes.97, votes.00, 
               votes.03, votes.06, votes.09, votes.12)
round(votes, 4)
seats <- rbind(seats.91, seats.94, seats.97, seats.00, 
               seats.03, seats.06, seats.09, seats.12)
print(seats)

